package com.situ.mall.service.impl;

import java.util.List;

import javax.servlet.http.HttpServlet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.mall.entity.User;
import com.situ.mall.entity.Result;
import com.situ.mall.mapper.UserMapper;
import com.situ.mall.service.IUserService;

@Service
public class UserServiceImpl implements IUserService{
	@Autowired
	private UserMapper userMapper;

// 0.测试
	@Override
	public List<User> selectAll() {
		return userMapper.selectAll();
	}
//后台
// 1.每页信息
	@Override
	public Result selectByPage(Integer page, Integer limit) {
		//1.总页数
		int count = userMapper.getTotalPage();
		
		// 2.每页信息
		int offset = (page-1)*limit;
		List<User> list = userMapper.selectByPage(offset, limit);
		
		System.out.println(count);
		System.out.println(list);
		
		return Result.success("", list, count);
	}
	
//2.删除 单个
	@Override
	public Result deleteById(Integer id) {
		System.out.println("UserServiceImpl.deleteById()");
		
		Integer count = userMapper.deleteById(id);
		return count>0 ? Result.success("删除成功") : Result.error("删除失败");
	}


//3.批量删除
	@Override
	public Result deleteAll(Integer[] ids) {
		System.out.println("UserServiceImpl.deleteAll()");
		
		//计数，总共删除的此时=ids的长度，删除成功，否则失败
		int count  = 0;
		for (Integer id : ids) {
			int ever = userMapper.deleteAll(id);
			count+=ever;
		}
		return count==ids.length ? Result.success("批量删除成功"):Result.error("批量删除失败");
	}


//4.添加
	@Override
	public Result insert(User user) {
		System.out.println("UserServiceImpl.insert()");
		
		int count = userMapper.insert(user);
		return count>0? Result.success("添加成功") : Result.error("添加失败");
	}
	


	
//前台  
	// 1.验证登陆信息   账户密码是否正确
	@Override
	public User login(User fromUser) {
		System.out.println("UserServiceImpl.login()");
	
		User user = userMapper.login(fromUser);
		System.out.println(user);
		return user; 
		
		
	}

}
